<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>装饰器计算一个函数的运行时间 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>装饰器计算一个函数的运行时间</h1>
  <time datetime=2020-06-18T18:01:48&#43;0800 class="post-date">Thu, Jun 18, 2020</time>
  <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> functools
<span style="color:#f92672">import</span> time


<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">run_time</span>(fn):
    <span style="color:#a6e22e">@functools.wraps</span>(fn)
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">wrapper</span>(<span style="color:#f92672">*</span>args, <span style="color:#f92672">**</span>kw):
        start <span style="color:#f92672">=</span> time<span style="color:#f92672">.</span>time()
        res <span style="color:#f92672">=</span> fn(<span style="color:#f92672">*</span>args, <span style="color:#f92672">**</span>kw)
        <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;</span><span style="color:#e6db74">%s</span><span style="color:#e6db74"> 运行了 </span><span style="color:#e6db74">%f</span><span style="color:#e6db74"> 秒&#39;</span> <span style="color:#f92672">%</span> (fn<span style="color:#f92672">.</span>__name__, time<span style="color:#f92672">.</span>time() <span style="color:#f92672">-</span> start))
        <span style="color:#66d9ef">return</span> res

    <span style="color:#66d9ef">return</span> wrapper


<span style="color:#75715e">#测试</span>
<span style="color:#a6e22e">@run_time</span>
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test</span>(n):
    time<span style="color:#f92672">.</span>sleep(n)
    <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;运行结束了&#34;</span>)


test(<span style="color:#ae81ff">3</span>)
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
